package ru.ds.selenium.applogic2;

import java.util.ArrayList;
import java.util.List;

import ru.ds.selenium.applogic.FilmHelper;
import ru.ds.selenium.model.Film;

public class FilmHelper2 extends DriverBasedHelper implements FilmHelper {

  public FilmHelper2(ApplicationManager2 manager) {
    super(manager.getWebDriver());
  }

  @Override
  public void create(Film film) {
	pages.mainPage.ensurePageLoaded().clickAddMovie();
    pages.addMoviePage.ensurePageLoaded()
    .setTitle(film.getTitle())
    .setYear(film.getYear())
    .setNotes(film.getNotes())
    .clickSubmitButton();
  }

  @Override
  public void delete(Film film) {
	if(film.getId() != null || "".equals(film.getId())) {  
		pages.mainPage.ensurePageLoaded().clickMovieById(film.getId());
	    pages.movieInfoPage.ensurePageLoaded().clickDelete();
	}    
  }

  @Override
  public List<Film> search(String title) {
	List<String> lstTitles = pages.mainPage.ensurePageLoaded().clearSearchField().performSearch(title);
	pages.mainPage.ensurePageLoaded().clearSearchField();
	List<Film> lstMovies = new ArrayList<Film>();
	for(String str : lstTitles) {
		lstMovies.add(new Film().setTitle(str));
	}
    return lstMovies;
  }

  @Override
  public boolean isCreated(Film film) {
	  if(film.getId() == null || "".equals(film.getId())) {
		  if(pages.movieInfoPage.isMovieInfoPage()) {
			  film.setId(pages.movieInfoPage.getMovieID());
			  return true;
		  } else {
			  return false;
		  }
	  } else {
		  return true;
	  }
  }

}
